<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/9
 * Time: 18:30
 */
namespace Controller;

use core\Redis;
use Ext\Server;
use Model\detailModel;
use Model\deviceModel;
use Model\testModel;
use Ext\Client;

class indexController
{
    public $params = null;
    //fd 和 uuid 各自进行删除

    public function initAction($params)
    {
        if (count(array_diff(['uuid'],array_keys($params)))>0){
            Server::failedSend($GLOBALS['fd'],[],ParamsRequiredError); return;
        }


        $redisHandel = Redis::getInstance();
        $redis = $redisHandel->get();

        $redis->hset(OnlineUuidToFd,$params['uuid'],$GLOBALS['fd']);
        $redis->hSet(OnlineFdToUuid,$GLOBALS['fd'],$params['uuid']);

        $redisHandel->put($redis);

        Server::successSend($GLOBALS['fd'],$params,SuccessMessage);

    }
    /**
     * websocket给对应客户端发送指令
     * @param $params
     */
    public function screenAction($params)
    {
        if (count(array_diff(['uuid','command'],array_keys($params)))>0){
            Server::failedSend($GLOBALS['fd'],[],ParamsRequiredError); return;
        }

        $redisHandel = Redis::getInstance();
        $redis = $redisHandel->get();

        $fd = $redis->hGet(OnlineUuidToFd,$params['uuid']);

        Server::successSend($fd,['command'=>$params['command']],SendScreenShotRequestSuccess);

    }


    /**
     * 指令回传
     * @param $params
     */
    public function shotAction($params){

        if (count(array_diff(['uuid','shot'],array_keys($params)))>0){
            Server::failedSend($GLOBALS['fd'],[],ParamsRequiredError); return;
        }


        $deviceModel = new deviceModel();

        $deviceModel->modelWhere(['uuid'=>$params['uuid']]);

        $deviceModel->modelUpdate(['pic'=>$params['shot']]);

        Client::send(['shot'=>$params['shot'],'uuid'=>$params['uuid']]);


    }

    /**
     * 设备状态更新接口
     * @param $params
     */
    public function offlineAction($params)
    {
        if (count(array_diff(['uuid','status'],array_keys($params)))>0){
            Server::failedSend($GLOBALS['fd'],[],ParamsRequiredError); return;
        }

        if(!in_array($params['status'],[0,1,2])){

            Server::failedSend($GLOBALS['fd'],
                [],ParamsRequiredError); return;
        }

        $redisHandel = Redis::getInstance();
        $redis = $redisHandel->get();

        $redis->hDel(OnlineUuidToFd,$params['uuid']);

        $deviceModel = new deviceModel();

        $deviceModel->modelWhere(['uuid'=>$params['uuid']]);

        $deviceModel->modelUpdate(['status'=>$params['status']]);


    }


    /**
     * 自动检测断线客户端
     */
    public function disconnect()
    {
        $connList = Server::getConnections();
        $redisHandle = Redis::getInstance();
        $redis = $redisHandle->get();

        $fdDevices = $redis->hVals(OnlineUuidToFd);
        $fdDevices = $fdDevices ?: [];

        $delFdDevices = array_diff($fdDevices,$connList);

        foreach ($delFdDevices as $fd){




        }

    }
}


